package me.zhengjie.modules.equipment.service.impl;

import me.zhengjie.modules.equipment.domain.TEquipment;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.equipment.repository.TEquipmentRepository;
import me.zhengjie.modules.equipment.service.TEquipmentService;
import me.zhengjie.modules.equipment.service.dto.TEquipmentDTO;
import me.zhengjie.modules.equipment.service.dto.TEquipmentQueryCriteria;
import me.zhengjie.modules.equipment.service.mapper.TEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author sw
* @date 2019-06-29
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TEquipmentServiceImpl implements TEquipmentService {

    @Autowired
    private TEquipmentRepository tEquipmentRepository;

    @Autowired
    private TEquipmentMapper tEquipmentMapper;

    @Override
    public Object queryAll(TEquipmentQueryCriteria criteria, Pageable pageable){
        Page<TEquipment> page = tEquipmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tEquipmentMapper::toDto));
    }

    @Override
    public Object queryAll(TEquipmentQueryCriteria criteria){
        return tEquipmentMapper.toDto(tEquipmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TEquipmentDTO findById(Integer id) {
        Optional<TEquipment> tEquipment = tEquipmentRepository.findById(id);
        ValidationUtil.isNull(tEquipment,"TEquipment","id",id);
        return tEquipmentMapper.toDto(tEquipment.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TEquipmentDTO create(TEquipment resources) {
        return tEquipmentMapper.toDto(tEquipmentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TEquipment resources) {
        Optional<TEquipment> optionalTEquipment = tEquipmentRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTEquipment,"TEquipment","id",resources.getId());

        TEquipment tEquipment = optionalTEquipment.get();
        // 此处需自己修改
        resources.setId(tEquipment.getId());
        tEquipmentRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tEquipmentRepository.deleteById(id);
    }

    @Override
    public List<TEquipmentDTO> getEqByCustomerId(int id) {
        List<TEquipment> list=tEquipmentRepository.getEqByCustomerId(String.valueOf(id));
        return list.stream().map(tEquipmentMapper::toDto).collect(Collectors.toList());
    }

    @Override
    public List<TEquipmentDTO> queryByCodeOrName(String code) {
        List<TEquipment> list=tEquipmentRepository.findByEqptcodeLikeOrEqptnameLike(code,code);
        return list.stream().map(tEquipmentMapper::toDto).collect(Collectors.toList());
    }

    @Override
    public List<TEquipmentDTO> queryByCodeAndCustomer(String code,String supplier) {
        List<TEquipment> list=tEquipmentRepository.findByEqptByCodeAndCustomer(code,supplier);
        return list.stream().map(tEquipmentMapper::toDto).collect(Collectors.toList());
    }
}